package controllers;

import play.*;
import play.mvc.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import models.*;

public class Application extends Controller {
	


    public static void index() {

        render();
    }
    
    public static void admin() {
    	render();
    }
    
    public static void invalidScreen() {
    	render();
    }
    
    public static void cadastro(){
    	render();
    }
    
    public static void busca()
    {
    	render();
    }
    
    public static void estoque(){
    	render();
    }
    public static void servico(){
    	render();
    }
    public static void agenda(){
    	render();
    }
    public static void gerenciar(){
    	render();
    }
    
    public static void telaSucesso() throws SQLException {//apenas para testes
    	List<Usuary> usuarios = new ArrayList<Usuary>();
    	
    	conectar();
    	ResultSet r;
    	
    	r = comando.executeQuery("SELECT id_u, nome, tipo FROM usuario");
    	while (r.next()) {
    		Usuary u = new Usuary(r.getInt("id_u"), r.getString("nome"), r.getString("tipo"));
    		usuarios.add(u);
    	}
    	
    	render(usuarios);
    }
    
    private static Connection con;  
	private static Statement comando;
	
	private static void conectar() throws SQLException {  
	      
  	  
		try {
			Class.forName("org.postgresql.Driver");
		} catch (ClassNotFoundException e) {
		}
	
			con = DriverManager.getConnection(  
			        "jdbc:postgresql://localhost:5432/gof",  
			        "postgres", "abc123");
		
			comando =  con.createStatement();

	}
	
	 public static void cadastrarUsuario(String nome, String login, String senha, String tipo) throws SQLException{
    
	
    	conectar();
    	comando.executeUpdate("INSERT INTO usuario (nome, login, senha, tipo) VALUES ( '" + nome + "','" + login + "','" + senha + "','" + tipo + "' )");
    	
    	index();
    }
	 
	 public static void cadastroCliente(String nome, String sex, int age, String rg, String cpf, String email, int tel, String nasc) throws SQLException{
		 
		 conectar();
		 String cmd = "'" + nome + "','" + sex + "'," + age + ",'" + rg + "','" + cpf + "','" + email + "'," + tel + ",'" + nasc + "'";
		 comando.executeUpdate("INSERT INTO cliente (nome, sexo, idade, rg, cpf, email, tel, nasc) VALUES ( " + cmd + " )");
	     index();
		 
	 }
	 
	 public static void fazerLogin(String login, String senha) throws SQLException {
		 
		 conectar();
		 
		 ResultSet rs;
		 
		 rs = comando.executeQuery("SELECT * FROM usuario WHERE login='" + login + "' AND senha='" + senha + "'");
		 
		 if (rs.next()) {
			admin();
		 }
		 else {
			 
			 flash.error("Login ou senha incorretos, tente novamente!");
			 index();

		 }
	 }

}